// index.js
const app = getApp()
var get_openid;var get_index;var wxtel;var get_user;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 800,
    mobile:'',
  },
  imgtap:function(e){
    var page = e.currentTarget.dataset.page;
    wx.navigateTo({
      url: page,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    get_openid = app.data.index_url + 'get_openid';
    get_index = app.data.index_url + 'get_index';
    wxtel = app.data.ucenter_url + 'wxtel';
    get_user = app.data.ucenter_url + 'get_user';
    //获取胶囊位置信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    var navTop;
    var navHeight;
    var model = '';
    //获取手机系统信息
    wx.getSystemInfo({
      success: res => {
        //导航高度
        model = res.model;
        var statusBarHeight = res.statusBarHeight;
        navTop = menuButtonObject.top;
        navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        var backHeight=(navTop*2+(navHeight-navTop))-45;
        console.log(backHeight);
        // if(model == "iPhone XS Max" || model == "iPhone X" || model == "iPhone XR" || model == "iPhone XS Max"){
        //   backHeight = backHeight - 24;
        // }
        var scrollHeight = backHeight - 5;
        that.setData({
          backHeight:backHeight,
          scrollHeight:scrollHeight,
          scor_height:menuButtonObject.height
        })
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  mobiletap:function(e){
    wx.makePhoneCall({
      phoneNumber: '18872292422'
    })
  },
  tradetap:function(e){
    wx.navigateTo({
      url: '/pages/i_trade/i_trade',
    })
  },
  xqtap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/i_detail/i_detail?id='+id,
    })
  },
  yhqtap:function(e){
    wx.navigateTo({
      url: '/pages/i_yhq/i_yhq',
    })
  },
  roomtap:function(e){
    wx.switchTab({
      url: '/pages/room/room',
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.encryptedData){
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log(res.code);
          var aid = wx.getStorageSync('uid');
          wx.request({
            url: wxtel,
            data: {
              code: res.code,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              uid:aid
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              console.log(res.data);
              if(res.data.code == 0){
                var mobile = res.data;
                app.data.mobile = mobile;
                that.setData({
                  mobile: mobile
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: '获取失败！',
                  showCancel: false
                })
              }
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: get_index,
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data);
        var b_list = res.data.b_list;//轮播图
        var o_list = res.data.o_list;
        var img_url = app.data.img_url;
        if(b_list.length != 0){
          for(var i=0;i<b_list.length;i++){
            b_list[i].image = img_url + b_list[i].image;
          }
        }
        that.setData({
          imgUrls:b_list,
          o_list:o_list
        })
      }
    })
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res.code);
        wx.showLoading();
        wx.request({
          url: get_openid,
          data: {
            code: res.code,
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);
            wx.hideLoading();
            var obj = res.data.openid;
            var aid = res.data.uid;
            wx.setStorageSync('user', obj);//存储openid 
            wx.setStorageSync('uid', aid);//存储uid
            wx.request({
              url: get_user,
              data: {
                uid:aid
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                console.log(res.data);
                var info = res.data.info;
                var mobile = info.mobile;
                var role_id = info.role_id;
                app.data.mobile = mobile;
                app.data.role_id = role_id;
                that.setData({
                  mobile:mobile
                })
              }
            })
          }
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})